package com.study.collection;

import org.apache.commons.compress.utils.Lists;

import java.util.*;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * lru cache实现
 * @param <K>
 * @param <V>
 */
public class LRUCache<K,V> extends LinkedHashMap<K,V> {

    private final int maxCache;

    public LRUCache(int capSize,int maxCache){
        super(capSize,maxCache);
        this.maxCache = maxCache;
    }

    protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
        return this.size()> this.maxCache;
    }

    public static void main(String[] args) {
         LRUCache  lruCache = new LRUCache(10,2);
         lruCache.put(1,2);
         lruCache.put(2,3);
         lruCache.put(3,4);
        System.out.println(lruCache.toString());

        Vector<String> vector = new Vector<>();
        vector.add("123");
        //高并发场景
        ConcurrentSkipListSet<String> concurrentSkipListSet = new ConcurrentSkipListSet<>();
        concurrentSkipListSet.add("123");

        ConcurrentLinkedQueue<String> concurrentLinkedQueue = new ConcurrentLinkedQueue<>();
        concurrentLinkedQueue.add("33");

        //List<String> ts = Collections.synchronizedList(lruCache);

        HashSet<String> set = new HashSet<>();
        set.add(null);
        set.add(null);


    //    CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<>();
        List<Integer> list = Collections.synchronizedList(new ArrayList<>());
      //       List<Integer> list = Lists.newArrayList();
        new Thread(()->{
            for (int i = 0;i<1000;i++){
                list.add(i);
               // System.out.println(i);
            }
        }).start();

                new Thread(()->{

                    for (int i = 0;i<1000;i++){

                        list.add(i);

                    }
                }).start();
        try{
            Thread.sleep(3000);
        }   catch (Exception e){

        }
        System.out.println(list.size());


    }
}
